// 封装数据库模块代码
const mysql = require('mysql');
const db_config = require('./db_config');

/**
 * sql String
 * data Array
 */
async function getDataBySQL(sql, data) {
    let conn = mysql.createConnection(db_config);
    conn.connect(err=>{
        if(err){
            console.log(err.message);
            return {status:201,msg:'数据库出差了，请稍后尝试!'};
        }
    });
    let rs=await new Promise((resolve,reject)=>{
        conn.query(sql,data,(err,result)=>{
            if(err)resolve([err,null]);
            else resolve([null,result]);
        })
    });
    conn.end(err=>{
        if(err){
            console.log(err.message);
            return {status:201,msg:'数据库下班了，请上班再试！'};
        }
    });
    return rs;
};

module.exports={
    getDataBySQL
}